<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var waysToSplit = function (a) {
      let n = a.length;
      let v = new Array(n + 1).fill(0);
      // 初始化前缀和数组
      for (let i = 1; i <= n; i++) {
        v[i] = v[i - 1] + a[i - 1];
      }
      // 返回值 ret
      let ret = 0;
      let M = 100000007;

      for (let i = 1; i < n; i++) {
        // 特判
        if (v[i] * 3 > v[n]) break;

        // 第一次二分找右边界
        let l = i + 1, r = n - 1;
        while (l <= r) {
          let mid = (l + r) >> 1;
          if (v[n] - v[mid] < v[mid] - v[i]) {
            r = mid - 1;
          } else {
            l = mid + 1;
          }
        }

        // 第二次二分找左边界
        let ll = i + 1, rr = n - 1;
        while (ll <= rr) {
          let mid = (ll + rr) >> 1;
          if (v[mid] - v[i] < v[i]) {
            ll = mid + 1;
          } else {
            rr = mid - 1;
          }
        }
        ret += (l - ll) % M;
      }
      return ret;
    };
    console.log(waysToSplit([1, 2, 2, 2, 5, 0]))
  </script>
</body>

</html>